﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;

namespace ConAppCore
{
    internal class TestStreamEx
    {
        public static void Test()
        {
            try
            {
                //Array arr = new double[2];
                //Array arr = new short[2];
                //arr.SetValue(1, 0);
                //arr.SetValue(2, 1);

                //TestRWBufferChar();
                //TestRWBufferByte();
                //TestRWBufferSbyte();
                //TestRWBufferInt16();
                //TestRWBufferUInt16();
                //TestRWBufferInt32();
                //TestRWBufferUInt32();
                //TestRWBufferInt64();
                //TestRWBufferUInt64();
                //TestRWBufferSingle();
                //TestRWBufferDouble();
                //TestRWBufferDecimal();

                //TestRWIQ1();
                //TestRWIQ2();
                //TestRWIQ3();


                TestReadOrWriteChars();
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestReadOrWriteChars()
        {
            Encoding coding = Encoding.UTF8;
            coding = null;

            string code = @"AAAAABBBBB0001";
            using var ms = new MemoryStream();
            var writer = new BinaryWriter(ms);
            StreamEx.WriteFixByteLengthString(writer, code, 14, coding);

            ms.Position = 0;
            var reader = new BinaryReader(ms);
            string code2 = StreamEx.ReadFixByteLengthString(reader, 14, coding);
        }

        private static void TestRWIQ3()
        {
            Complex[] iqDataArr = new Complex[]
            {
                new Complex(1,-1),
                new Complex(2,-2),
                new Complex(3,-3),
                new Complex(4,-4),
                new Complex(5,-5),
            };

            int startIndex = 2;
            int count = 2;
            using (var msI = new MemoryStream())
            {
                using (var msQ = new MemoryStream())
                {
                    //StreamEx.WriteIQ<short>(ms, iDataArr, qDataArr);
                    //ms.Position = 0;
                    //Complex[] iqDataArr = StreamEx.ReadIQ(ms, iDataArr.Length, TypeCode.Int16);

                    StreamEx.WriteIQ(msI, msQ, iqDataArr, startIndex, count);
                    msI.Position = 0;
                    msQ.Position = 0;
                    Complex[] iqDataArr2 = StreamEx.ReadIQ(msI, msQ, count, TypeCode.Int16);

                    msI.Position = 0;
                    msQ.Position = 0;
                    short[] iDataArr = new short[count];
                    short[] qDataArr = new short[count];
                    StreamEx.ReadIQ(msI, msQ, iDataArr, qDataArr, 0, count, TypeCode.Int16);


                    msI.Position = 0;
                    msQ.Position = 0;
                    StreamEx.WriteIQ(msI, msQ, iDataArr, qDataArr);

                    msI.Position = 0;
                    msQ.Position = 0;
                    short[] iDataArr2 = new short[count];
                    short[] qDataArr2 = new short[count];
                    StreamEx.ReadIQ(msI, msQ, iDataArr2, qDataArr2, 0, count, TypeCode.Int16);
                }
            }
        }

        private static void TestRWIQ2()
        {
            Complex[] iqDataArr = new Complex[]
            {
                new Complex(1,-1),
                new Complex(2,-2),
                new Complex(3,-3),
                new Complex(4,-4),
                new Complex(5,-5),
            };

            int startIndex = 2;
            int count = 2;
            using (var ms = new MemoryStream())
            {
                //StreamEx.WriteIQ<short>(ms, iDataArr, qDataArr);
                //ms.Position = 0;
                //Complex[] iqDataArr = StreamEx.ReadIQ(ms, iDataArr.Length, TypeCode.Int16);

                StreamEx.WriteIQ(ms, iqDataArr, startIndex, count);
                ms.Position = 0;
                Complex[] iqDataArr2 = StreamEx.ReadIQ(ms, count, TypeCode.Int16);

                ms.Position = 0;
                short[] iDataArr = new short[count];
                short[] qDataArr = new short[count];
                StreamEx.ReadIQ(ms, iDataArr, qDataArr, 0, count, TypeCode.Int16);
            }
        }

        private static void TestRWIQ1()
        {
            short[] iDataArr = new short[] { 1, 2, 3, 4, 5 };
            short[] qDataArr = new short[] { -1, -2, -3, -4, -5 };

            using (var ms = new MemoryStream())
            {
                //StreamEx.WriteIQ<short>(ms, iDataArr, qDataArr);
                //ms.Position = 0;
                //Complex[] iqDataArr = StreamEx.ReadIQ(ms, iDataArr.Length, TypeCode.Int16);

                StreamEx.WriteIQ<short>(ms, iDataArr, qDataArr, 2, 2);
                ms.Position = 0;
                Complex[] iqDataArr = StreamEx.ReadIQ(ms, 2, TypeCode.Int16);
            }
        }

        private static void TestRWBufferChar()
        {
            char[] data = new char[] { 'a', 'b', 'c', 'd' };
            using (var ms = new MemoryStream())
            {
                var writer = new BinaryWriter(ms);
                StreamEx.WriteBuffer<char>(writer, data);

                ms.Position = 0;
                var reader = new BinaryReader(ms);
                char[] data2 = StreamEx.ReadBuffer<char>(reader, TypeCode.Char, data.Length);
            }
        }

        private static void TestRWBufferByte()
        {
            byte[] data = new byte[] { 10, 23, 45, 60 };
            using (var ms = new MemoryStream())
            {
                var writer = new BinaryWriter(ms);
                StreamEx.WriteBuffer<byte>(writer, data);

                ms.Position = 0;
                var reader = new BinaryReader(ms);
                byte[] data2 = StreamEx.ReadBuffer<byte>(reader, TypeCode.Byte, data.Length);
            }
        }

        private static void TestRWBufferDecimal()
        {
            try
            {
                var buffer1 = new decimal[] { -10.23m, 100.123m, 1000.45m };
                using (var ms = new MemoryStream())
                {
                    var bw = new BinaryWriter(ms);
                    StreamEx.WriteBuffer(bw, buffer1);

                    ms.Position = 0;
                    var br = new BinaryReader(ms);
                    var buffer2 = new decimal[buffer1.Length];
                    StreamEx.ReadBuffer(br, TypeCode.Decimal, buffer2);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestRWBufferDouble()
        {
            try
            {
                var buffer1 = new double[] { -10.23d, 100.36d, 1001.23d };
                using (var ms = new MemoryStream())
                {
                    var bw = new BinaryWriter(ms);
                    StreamEx.WriteBuffer(bw, buffer1);

                    ms.Position = 0;
                    var br = new BinaryReader(ms);
                    var buffer2 = new double[buffer1.Length];
                    StreamEx.ReadBuffer(br, TypeCode.Double, buffer2);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestRWBufferSingle()
        {
            try
            {
                var buffer1 = new float[] { -10.2356f, 100.45f, 1000 };
                using (var ms = new MemoryStream())
                {
                    var bw = new BinaryWriter(ms);
                    StreamEx.WriteBuffer(bw, buffer1);

                    ms.Position = 0;
                    var br = new BinaryReader(ms);
                    var buffer2 = new float[buffer1.Length];
                    StreamEx.ReadBuffer(br, TypeCode.Single, buffer2);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestRWBufferUInt64()
        {
            try
            {
                var buffer1 = new UInt64[] { 10, 100, 1000 };
                using (var ms = new MemoryStream())
                {
                    var bw = new BinaryWriter(ms);
                    StreamEx.WriteBuffer(bw, buffer1);

                    ms.Position = 0;
                    var br = new BinaryReader(ms);
                    var buffer2 = new UInt64[buffer1.Length];
                    StreamEx.ReadBuffer(br, TypeCode.UInt64, buffer2);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestRWBufferInt64()
        {
            try
            {
                var buffer1 = new Int64[] { -10, 100, 1000 };
                using (var ms = new MemoryStream())
                {
                    var bw = new BinaryWriter(ms);
                    StreamEx.WriteBuffer(bw, buffer1);

                    ms.Position = 0;
                    var br = new BinaryReader(ms);
                    var buffer2 = new Int64[buffer1.Length];
                    StreamEx.ReadBuffer(br, TypeCode.Int64, buffer2);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestRWBufferUInt32()
        {
            try
            {
                var buffer1 = new UInt32[] { 10, 100, 1000 };
                using (var ms = new MemoryStream())
                {
                    var bw = new BinaryWriter(ms);
                    StreamEx.WriteBuffer(bw, buffer1);

                    ms.Position = 0;
                    var br = new BinaryReader(ms);
                    var buffer2 = new UInt32[buffer1.Length];
                    StreamEx.ReadBuffer(br, TypeCode.UInt32, buffer2);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestRWBufferInt32()
        {
            try
            {
                var buffer1 = new Int32[] { -10, 100, 1000 };
                using (var ms = new MemoryStream())
                {
                    var bw = new BinaryWriter(ms);
                    StreamEx.WriteBuffer(bw, buffer1);

                    ms.Position = 0;
                    var br = new BinaryReader(ms);
                    var buffer2 = new Int32[buffer1.Length];
                    StreamEx.ReadBuffer(br, TypeCode.Int32, buffer2);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestRWBufferUInt16()
        {
            try
            {
                var buffer1 = new UInt16[] { 10, 100, 1000 };
                using (var ms = new MemoryStream())
                {
                    var bw = new BinaryWriter(ms);
                    StreamEx.WriteBuffer(bw, buffer1);

                    ms.Position = 0;
                    var br = new BinaryReader(ms);
                    var buffer2 = new UInt16[buffer1.Length];
                    StreamEx.ReadBuffer(br, TypeCode.UInt16, buffer2);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestRWBufferInt16()
        {
            try
            {
                var buffer1 = new Int16[] { -10, 0, 10 };
                using (var ms = new MemoryStream())
                {
                    var bw = new BinaryWriter(ms);
                    StreamEx.WriteBuffer(bw, buffer1);

                    ms.Position = 0;
                    var br = new BinaryReader(ms);
                    var buffer2 = new Int16[buffer1.Length];
                    StreamEx.ReadBuffer(br, TypeCode.Int16, buffer2);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestRWBufferSbyte()
        {
            try
            {
                var buffer1 = new sbyte[] { -10, 0, 10 };
                using (var ms = new MemoryStream())
                {
                    var bw = new BinaryWriter(ms);
                    StreamEx.WriteBuffer(bw, buffer1);

                    ms.Position = 0;
                    var br = new BinaryReader(ms);
                    var buffer2 = new sbyte[buffer1.Length];
                    StreamEx.ReadBuffer(br, TypeCode.SByte, buffer2);
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }
    }
}
